/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.resource;

import cz.fi.muni.diplomka.common.enumeration.ResourceType;
import cz.fi.muni.diplomka.common.enumeration.UnitType;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityResourcesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityUnitsManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityResourcesManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityUnitsManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class ResourcesPage extends WebPage {
    
    private CityResourcesManager cityResourcesManager = new CityResourcesManagerImpl();
    private CityUnitsManager cityUnitsManager = new CityUnitsManagerImpl();
    private CityManager cityManager = new CityManagerImpl();
    private String selectedCity = "";
    private List<String> cities;
    
    public ResourcesPage(PageParameters parameters) {
        final Long id;
        
        add(new FeedbackPanel("feedback"));
        
        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));
            
            PageParameters p = new PageParameters();
            p.add("id", parameters.getString("id"));
            add(new LinkPanel("linkPanel", p));
            
            cities = new ArrayList<>();
            for (City c : cityManager.getCitiesByPlayerId(id)) {
                cities.add(c.getName());
            }
            if (parameters.containsKey("cityName")) {
            selectedCity = parameters.getString("cityName");
            }
            
            if(selectedCity.equals("")){
                selectedCity = cities.get(0).toString();
            }
            
            final DropDownChoice<String> city = new DropDownChoice<>("city",
                    new PropertyModel<String>(this, "selectedCity"), cities);
            
            
            Form<?> form = new Form<Void>("form") {
                @Override
                protected void onSubmit() {
                     selectedCity = cities.get(Integer.parseInt(city.getValue()));
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", id.toString());
                    pageParameters.add("cityName", selectedCity);
                    setResponsePage(ResourcesPage.class, pageParameters);
                }
            };
            
            add(form);
            form.add(city);
            
            
            List<String> list = new ArrayList<>();
                              
                for (ResourceType resource : ResourceType.values()) {

                    String add = resource.toString() + "%"
                            + cityResourcesManager.getResourceCount(cityManager.getCityByName(selectedCity).getId(), resource)
                            + "%" + cityResourcesManager.getMaximumPossibleResourceCount(cityManager.getCityByName(selectedCity).getId(), resource)
                            + "%" + cityResourcesManager.getResourceProductionCount(cityManager.getCityByName(selectedCity).getId(), resource);
                    list.add(add);
                }                    


            ListView listview = new ListView("listview", list) {
                @Override
                protected void populateItem(ListItem item) {
                    String string = (String) item.getModelObject();
                    String[] tokens = string.split("%");
                    item.add(new Label("resource", tokens[0]));
                    item.add(new Label("count", tokens[1]));
                    item.add(new Label("capacity", tokens[2]));
                    item.add(new Label("production", tokens[3]));
                    
                }
            };
            
            add(listview);
            add(new Label("cityName", selectedCity));
            
             List<String> units = new ArrayList<>();
                              
                for (UnitType unitType : UnitType.values()) {

                    String add = unitType.toString() + "%"
                            + cityUnitsManager.getUnitCount(cityManager.getCityByName(selectedCity).getId(), unitType);
                    units.add(add);
                }                    


            ListView unitsView = new ListView("units", units) {
                @Override
                protected void populateItem(ListItem item) {
                    String string = (String) item.getModelObject();
                    String[] tokens = string.split("%");
                    item.add(new Label("name", tokens[0]));
                    item.add(new Label("unitCount", tokens[1]));

                }
            };
            add(unitsView);
        }
    }
}
